package com.njts.servlet;
import com.njts.auto.AutoCreateBean;
import com.njts.auto.AutoSearch;
import com.njts.bean.Class;
import com.njts.bean.Student;
import com.njts.dao.UseDao;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

@WebServlet("/studentUpdate")
public class StudentUpdateServlet extends HttpServlet {
    AutoCreateBean autoCreateBean=new AutoCreateBean();
    AutoSearch autoSearch=new AutoSearch();
    UseDao useDao=new UseDao();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        Student student=new Student();
        String setComplementSql="";
        try {
            student =(Student) autoCreateBean.createBean(req, student);
            setComplementSql = autoSearch.getSetComplementSql(student);
        } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        useDao.updateStudent(setComplementSql, student.getId());
        //
        ArrayList<Student> studentList= (ArrayList<Student>) useDao.selectStudent();
        ArrayList<Class> classList=(ArrayList<Class>) useDao.selectClass();
        req.setAttribute("classList",classList);
        req.setAttribute("studentList",studentList);
        req.setAttribute("studentCount",20); //先写死后面改
        req.getRequestDispatcher("/view/student.jsp").forward(req, resp);
    }
}
